import time

from selenium.webdriver.common.by import By
from common.Ultils import BlogDriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#测试博客详情页
class BlogDeail:
    driver = ""
    url = ""
    def __init__(self):
        url="http://8.137.19.140:9090/blog_detail.html?blogId=44779"
        self.driver=BlogDriver.driver
        self.driver.get(url)
    def DetailTestByLogin(self):
        # 截屏操作
        BlogDriver.getScreeShot()
        # 检查标题
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.title")
        # 检查时间
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.date")
        #检查内容
        self.driver.find_element(By.CSS_SELECTOR,"#h2--")
        # self.driver.back()

    def DetailTestByNoLogin(self):
        #没有弹窗出现
        #捕获弹窗
        # # 显示等待
        # WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        # alert=self.driver.switch_to.alert
        # alert_text=alert.text
        # print(f"弹窗内容: {alert_text}")
        # alert.accept()

        # # 隐式等待
        # self.driver.implicitly_wait(5)

        # #显示等待
        # WebDriverWait(self.driver, 5).until(EC.visibility_of_element_located(By.CSS_SELECTOR, "body > div.container-login > div > h3"))
        # 显式等待元素可见
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(
                (By.CSS_SELECTOR, "body > div.container-login > div")  # 注意这里是元组
            )
        )
        # 截屏操作
        BlogDriver.getScreeShot()

        username = self.driver.find_element(By.CSS_SELECTOR, "#username").text
        password = self.driver.find_element(By.CSS_SELECTOR, "#password").text
        assert username == "" and password == ""
